package com.wswy.wzcx.ui.car.remind

import android.animation.ValueAnimator
import android.arch.lifecycle.Observer
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import com.wswy.wzcx.R
import com.wswy.wzcx.model.car.CarStatus
import com.wswy.wzcx.model.car.InsuranceInfo
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import kotlinx.android.synthetic.main.fragment_info_insurance.*
import kotlin.math.abs

/**
 * 保险提醒
 */
class InsuranceInfoFragment : CBaseFragment() {

    companion object {
        private const val TAG = "InsuranceInfoFragment"
    }

    private lateinit var remindViewModel: RemindViewModel

    private var nextShow:(()->Unit)?=null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_info_insurance,container,false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        remindViewModel = provideViewModelFromActivity(RemindViewModel::class.java)

        tv_car_no.text = remindViewModel.userCarInfo.carNo

        remindViewModel.userCarInfo.carExtendInfo?.insuranceInfo?.let {
            showInfo(it)
        }


        remindViewModel.getAddInsuranceLiveData().observe(this, Observer {
            it?.run {

                if (isOk()){
                   data?.insuranceInfo?.run {
                       showInfo(this)
                   }
                }
            }
        })

        //编辑
        img_edit.setOnClickListener {

            StatTools.sendClick(context, StatisticsId.addinsurance_edit)

            (activity as? RemindActivity)?.run {
                showAdd()
            }
        }

        //立即续保
        btn_insurance.setOnClickListener {

            remindViewModel.userCarInfo.carExtendInfo?.insuranceInfo?.link?.run {

                if (!TextUtils.isEmpty(this)){
                    startActivity(RouterUtils.getWebIntent(it.context,this))
                }

            }


            StatTools.sendClick(context, StatisticsId.addinsurance_renew)
        }

    }


    private fun showInfo(info: InsuranceInfo){


        tv_last_company.text = getString(R.string.insurance_company,info.company)

        if (isHidden){
            nextShow = {
                showDays(info)
            }
        }else{
            showDays(info)
        }

        when(info.status){
            CarStatus.NORMAL ->{
                //未到投保期
                val color = ContextCompat.getColor(tv_last_company.context,R.color.textColorPrimary_40)
                tv_insurance_remain_days.setTextColor(ContextCompat.getColor(tv_last_company.context,R.color.textColorPrimary))


                tv_insurance_remain_days_hint.text = "距离保险到期天数"


                tv_insurance_remain_days_hint.setTextColor(color)

                tv_insurance_desc_title.text = "目前不需要续保，可以放心驾驶"
                tv_insurance_desc_hint.text = info.desc
                btn_insurance.isEnabled = false
                btn_insurance.text = "暂无需续保"
            }
            CarStatus.PROMPT -> {
                //已到保险期

                val color = ContextCompat.getColor(tv_last_company.context,R.color.accentColor1)
                tv_insurance_remain_days.setTextColor(color)

                tv_insurance_remain_days_hint.text = "距离保险到期天数"
                tv_insurance_remain_days_hint.setTextColor(color)

                tv_insurance_desc_title.text = "当前已进入续保周期，请及时续保"
                tv_insurance_desc_hint.text = info.desc

                btn_insurance.isEnabled = true
                btn_insurance.text = "立即续保"
            }
            CarStatus.OVERDUE -> {
                //已过投保期

                val color = ContextCompat.getColor(tv_last_company.context,R.color.accentColor3)
                tv_insurance_remain_days.setTextColor(color)

                tv_insurance_remain_days_hint.text = "保险逾期天数"
                tv_insurance_remain_days_hint.setTextColor(color)
                tv_insurance_desc_title.text = "您的车辆保险已过期，请及时续保"
                tv_insurance_desc_hint.text = "请尽快为您的爱车续保，以免出事故无法出险"

                btn_insurance.isEnabled = true
                btn_insurance.text = "立即续保"
            }
        }

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)

        if (!hidden && nextShow != null){
            remindViewModel.userCarInfo.carExtendInfo?.insuranceInfo?.run {
                nextShow?.invoke()

                nextShow = null
            }
        }
    }

    private fun showDays(info: InsuranceInfo){

        if (isHidden){
            return
        }
        val days =  abs(info.remainDay)

        tv_insurance_remain_days?.text = 0.toString()


        ValueAnimator.ofInt(0,days).apply {
            duration = 1000
            interpolator = AccelerateDecelerateInterpolator()

            addUpdateListener {

                (it.animatedValue as? Int)?.run {
                    tv_insurance_remain_days?.text = toString()
                }

            }


        }.run {
            start()
        }

    }

}